{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Exchange LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: exchangeBits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #配对交换"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>配对交换。编写程序，交换某个整数的奇数位和偶数位，尽量使用较少的指令（也就是说，位0与位1交换，位2与位3交换，以此类推）。</p>\n",
    "\n",
    "<p> <strong>示例1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong> 输入</strong>：num = 2（或者0b10）\n",
    "<strong> 输出</strong> 1 (或者 0b01)\n",
    "</pre>\n",
    "\n",
    "<p> <strong>示例2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong> 输入</strong>：num = 3\n",
    "<strong> 输出</strong>：3\n",
    "</pre>\n",
    "\n",
    "<p> <strong>提示:</strong></p>\n",
    "\n",
    "<ol>\n",
    "<li><code>num</code>的范围在[0, 2^30 - 1]之间，不会发生整数溢出。</li>\n",
    "</ol>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [exchange-lcci](https://leetcode.cn/problems/exchange-lcci/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [exchange-lcci](https://leetcode.cn/problems/exchange-lcci/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xAAAAAAAA) >>1) | ((num & 0x55555555) <<1)\n",
    "\n",
    "        # bn = bin(num)[2:]\n",
    "        # res = [0] * len(bn)\n",
    "        # res[::2], res[1::2] = bn[1::2], bn[::2]\n",
    "        # return int(''.join(res), 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        binnum=bin(num )[2:]\n",
    "        outstr=\"\"\n",
    "        bit=len(binnum)-1\n",
    "\n",
    "        while bit>0:\n",
    "            if binnum[bit]!=binnum[bit-1]:\n",
    "                outstr=binnum[bit]+binnum[bit-1]+outstr\n",
    "            else:\n",
    "                outstr=binnum[bit-1]+binnum[bit]+outstr\n",
    "\n",
    "            bit-=2\n",
    "        \n",
    "        if bit==0:\n",
    "            if binnum[0]==\"0\":\n",
    "                outstr=\"0\"\n",
    "            else:\n",
    "                outstr=\"10\"+outstr\n",
    "\n",
    "        return int(outstr,2)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        for i in range(0, 32, 2):\n",
    "            even_pos = num & (1 << i)\n",
    "            odd_pos = num & (1 << (i + 1))\n",
    "            if even_pos:\n",
    "                num |= (1 << (i + 1))\n",
    "            else:\n",
    "                num &= ~(1 << (i + 1))\n",
    "            if odd_pos:\n",
    "                num |= (1 << i)\n",
    "            else:\n",
    "                num &= ~(1 << i)\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        bitnum = bin(num)[2: ]\n",
    "        if len(bitnum) % 2:\n",
    "            bitnum = \"0\" + bitnum\n",
    "        resnum = \"\"\n",
    "        for i in range(len(bitnum) // 2):\n",
    "            resnum += bitnum[2 * i + 1] + bitnum[2 * i]\n",
    "        return int(resnum, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa)>> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        a = num & 0x55555555\n",
    "        b = num & 0xaaaaaaaa\n",
    "        return a << 1 | b >> 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        a,b=0xaaaaaaaa,0x55555555\n",
    "        return ((num&a)>>1)|((num&b)<<1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        res = 0\n",
    "        shift2dict = {0:0, 1:2, 2:1, 3: 3}\n",
    "        count = 0\n",
    "        while num:\n",
    "            left = num %4\n",
    "            res += shift2dict[left]* 4**count \n",
    "            num //= 4\n",
    "            count += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        odd = even = cnt = 0\n",
    "        while cnt < 30 and num >= (1 << cnt):\n",
    "            odd |= num & (1 << cnt)\n",
    "            even |= num & (1 << (cnt + 1))\n",
    "            cnt += 2\n",
    "        return (odd << 1) | (even >> 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num&0xaaaaaaaa)>>1)|((num&0x55555555)<<1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return (num & 0x55555555) << 1 | (num & 0xaaaaaaaa) >> 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        s = str(bin(num))[2:]\n",
    "        n = len(s)\n",
    "        if n%2 == 1:\n",
    "            s = '0'+s\n",
    "            n += 1\n",
    "        res = 0\n",
    "        a, b = n-2, n-1\n",
    "        for i in s[::2]:\n",
    "            if i == '1':\n",
    "                res += 2**a\n",
    "            a -= 2\n",
    "        for j in s[1::2]:\n",
    "            if j == '1':\n",
    "                res += 2**b\n",
    "            b -= 2\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        sNum = bin(num)[2:]\n",
    "        l = len(sNum)\n",
    "        if l % 2:\n",
    "            sNum = '0' + sNum\n",
    "        ans = ''\n",
    "        for idx in range(0,l,2):\n",
    "            ans += sNum[idx+1] + sNum[idx]\n",
    "        return int(ans,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        digts = []\n",
    "        while num:\n",
    "            digts.append(num % 2)\n",
    "            num = num >> 1\n",
    "        for i in range(0, len(digts), 2):\n",
    "            if i + 1 >= len(digts):\n",
    "                digts[i] = 0\n",
    "                digts += [1]\n",
    "            else:\n",
    "                digts[i], digts[i+1] = digts[i+1], digts[i]\n",
    "        ans = sum([digts[i] * 2**i for i in range(len(digts))])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return (((num & 0x55555555) << 1) | ((num & 0xaaaaaaaa) >> 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num&0x55555555) << 1) | ((num&0xaaaaaaaa) >> 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        odd = even = cnt = 0\n",
    "        while cnt < 30 and num >= (1 << cnt):\n",
    "            odd |= num & (1 << cnt)\n",
    "            even |= num & (1 << (cnt + 1))\n",
    "            cnt += 2\n",
    "        return (odd << 1) | (even >> 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        s = list(str(bin(num))[2:])\n",
    "        \n",
    "        s.reverse()\n",
    "        if len(s)%2==1:\n",
    "            s.append('0')\n",
    "        # print(s)\n",
    "        for i in range(0, len(s), 2):\n",
    "            s[i], s[i+1] = s[i+1], s[i]\n",
    "        # print(s)\n",
    "        s.reverse()\n",
    "        return int(''.join(s), 2)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        ans = 0\n",
    "        m = {\n",
    "            0: 0,\n",
    "            1: 2,\n",
    "            2: 1,\n",
    "            3: 3,\n",
    "        }\n",
    "        base = 1\n",
    "        while num > 0:\n",
    "            # 一下取两位\n",
    "            t = num % 4\n",
    "            num //= 4\n",
    "            ans = m[t]*base + ans\n",
    "            base *= 4\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa)>>1)|((num & 0x55555555)<<1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        even = num & int(\"101010101010101010101010101010\", 2)\n",
    "        odd = num & int(\"10101010101010101010101010101\", 2)\n",
    "        return (even >> 1) + (odd << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        JS = 0xaaaaaaaa # 101010\n",
    "        OS = 0x55555555 # 010101\n",
    "        return (num & JS) >> 1 | (num & OS) << 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# ### my:掩码\n",
    "# class Solution:\n",
    "#     def exchangeBits(self, num: int) -> int:\n",
    "#         msk1=0x55555555\n",
    "#         msk2=0xAAAAAAAA\n",
    "#         return ((num&msk1)<<1)|((num&msk2)>>1)\n",
    "\n",
    "### 网：掩码\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)\n",
    "\n",
    "# 作者：腐烂的橘子\n",
    "# 链接：https://leetcode.cn/problems/exchange-lcci/\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0x55555555) << 1)|((num & 0xaaaaaaaa) >> 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        odd = even = cnt = 0\n",
    "        while cnt < 30 and num >= (1 << cnt):\n",
    "            odd |= num & (1 << cnt)\n",
    "            even |= num & (1 << (cnt + 1))\n",
    "            cnt += 2\n",
    "        return (odd << 1) | (even >> 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        # digts = []\n",
    "        # while num:\n",
    "        #     digts.append(num % 2)\n",
    "        #     num = num >> 1\n",
    "        # for i in range(0, len(digts), 2):\n",
    "        #     if i + 1 >= len(digts):\n",
    "        #         digts[i] = 0\n",
    "        #         digts += [1]\n",
    "        #     else:\n",
    "        #         digts[i], digts[i+1] = digts[i+1], digts[i]\n",
    "        # ans = sum([digts[i] * 2**i for i in range(len(digts))])\n",
    "        # return ans\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        mask_radix = 0b01010101010101010101010101010101\n",
    "        mask_even  = 0b10101010101010101010101010101010\n",
    "        num_radix = num & mask_radix\n",
    "        num_even  = num & mask_even\n",
    "        num_radix = num_radix << 1\n",
    "        num_even  = num_even >> 1\n",
    "        return num_even | num_radix\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        msk1=0x55555555\n",
    "        msk2=0xAAAAAAAA\n",
    "        return ((num&msk1)<<1)|((num&msk2)>>1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        bits = [0] * 30\n",
    "        temp = num\n",
    "        for i in range(30):\n",
    "            bits[i] = temp % 2\n",
    "            temp //= 2\n",
    "        for i in range(15):\n",
    "            bits[i * 2], bits[i * 2 + 1] = bits[i * 2 + 1], bits[i * 2]\n",
    "        new_num = 0\n",
    "        for i in range(29, -1, -1):\n",
    "            new_num = new_num * 2 + bits[i]\n",
    "        return new_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 715827882) >> 1) | ((num & 357913941) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        odd = 0x55555555\n",
    "        even = 0xaaaaaaaa\n",
    "        return (num & odd) << 1 | (num & even) >> 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        as_str = list(bin(num)[2:].zfill(32))\n",
    "\n",
    "        for i in range(1, len(as_str), 2):\n",
    "            as_str[i], as_str[i - 1] = as_str[i - 1], as_str[i]\n",
    "\n",
    "        return int(''.join(as_str), 2)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        n = bin(num)[2:]\n",
    "        if len(n)%2:\n",
    "            num = list('0'+n)\n",
    "        else:\n",
    "            num = list(n)\n",
    "        for i in range(0,len(num),2):\n",
    "            num[i], num[i+1] = num[i+1], num[i]\n",
    "        return int(''.join(num),base=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        odd = 0x55555555\n",
    "        even = 0xaaaaaaaa\n",
    "        return (num & odd) << 1 | (num & even) >> 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa)>>1 | (num&0x55555555)<<1)\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        # res = 0\n",
    "        # for i in range(0,32,2):\n",
    "        #     tmp1 = (num>>i)&1\n",
    "        #     tmp2 = (num>>(i+1))&1\n",
    "        #     if tmp1==1:\n",
    "        #         res|=(1<<(i+1))\n",
    "        #     if tmp2==1:\n",
    "        #         res|=(1<<i)\n",
    "        # return res\n",
    "\n",
    "        return ((num&0x55555555)<<1)|((num&0xaaaaaaaa)>>1)\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num&0x55555555) << 1) | ((num&0xaaaaaaaa) >> 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        result1 = num & 0x55555555\n",
    "        result2 = num & 0xaaaaaaaa\n",
    "\n",
    "        result1 = result1 << 1\n",
    "        result2 = result2 >> 1\n",
    "\n",
    "        return result1 | result2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        num = bin(num)\n",
    "        if num.count('1')+num.count('0')%2!=0:\n",
    "            num=num.replace('b','0',)\n",
    "            num=num.replace('0','o',1)\n",
    "        num = list(num)\n",
    "        for i in range(len(num)-1,0,-2):\n",
    "            t = num[i]\n",
    "            num[i] = num[i-1]\n",
    "            num[i-1] = t\n",
    "        print(num)\n",
    "        s,q = 0,0\n",
    "        for i in range(len(num)-1,0,-1):\n",
    "            if num[i]=='0' or num[i]=='1':\n",
    "                s+=int(num[i])*pow(2,q)\n",
    "                q+=1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        odd = 0x55555555\n",
    "        even = 0xaaaaaaaa\n",
    "        return (num & odd) << 1 | (num & even) >> 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa) >> 1) | ((num & 0x55555555) << 1);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((num & 0xaaaaaaaa)>>1) | ((num & 0x55555555) << 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def exchangeBits(self, num: int) -> int:\n",
    "        return ((0x55555555 & num) << 1) | ((0xaaaaaaaa & num) >> 1)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
